/*9XzYRF6NPZv70AU8R8S+r3R2QZMnUz9oYboPLb7hFlQFajNd3stjH7RMl9uBiEjQ*/

import { URL, PAGECODE, STATUS, DataSource } from '../../constance';
import initButtonState from '../init/initButtonState';

/**
 * 卡片详情页查询
 * @param {*} pageModel 
 * @param {*} pk 
 */
export default function (pageModel, pk) {
    const { dealFormulamsg, ajax, toast, spaCache } = pageModel;
    const { getCacheById, addCache } = spaCache;
    if (!pk) {
        initButtonState(pageModel)
        return;
    };
    const dataSource = DataSource;
    let cardData = getCacheById(pk, dataSource);
    if (cardData) {
        setPageData(pageModel, cardData);
    } else {
        const conditionData = {
            pk,
            pagecode: PAGECODE.cardCode,
            status: pageModel.queryPageStatus(),
        };

        ajax({
            url: URL.queryCard,
            data: conditionData,
            method: 'POST',
            success: (res) => {
                const { data, formulamsg } = res;
                if (Array.isArray(formulamsg) && formulamsg.length) {
                    dealFormulamsg(
                        res.formulamsg //参数一：返回的公式对象
                    );
                }
                setPageData(pageModel, data);
                addCache(pk, data, PAGECODE.cardHead, dataSource);//卡片页增加缓存
            },
            error: (error) => {
                toast(error.message); /* 国际化处理： 注意*/
            }
        });
    }
}

/**
 * 页面设置数据
 * @param {*} pageModel 
 * @param {*} data 
 * @returns 
 */
function setPageData(pageModel, data) {
    if (!data) return;
    data.head && pageModel.get(PAGECODE.cardHead).setData({ [PAGECODE.cardHead]: data.head[PAGECODE.cardHead] });
    if (data.bodys && data.bodys[PAGECODE.cardbody1]) {
        pageModel.get(PAGECODE.cardbody1).setData(data.bodys[PAGECODE.cardbody1]);
    } else {
        pageModel.get(PAGECODE.cardbody1).setData({ rows: [] });
    }
    if (data.bodys && data.bodys[PAGECODE.cardbody2]) {
        pageModel.get(PAGECODE.cardbody2).setData(data.bodys[PAGECODE.cardbody2]);
    } else {
        pageModel.get(PAGECODE.cardbody2).setData({ rows: [] });
    }
    initButtonState(pageModel);
}
/*9XzYRF6NPZv70AU8R8S+r3R2QZMnUz9oYboPLb7hFlQFajNd3stjH7RMl9uBiEjQ*/